package thread.notifydemo;

import java.util.concurrent.atomic.AtomicInteger;

public class NotifyDemo {
    public static Object object = new Object();
    static AtomicInteger value = new AtomicInteger(1);

    public static void main(String[] args) throws Exception {

        Thread t1 = new Thread(() -> {
            synchronized (object) {
                int res = value.get();
                while (res == 1) {
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                value.getAndIncrement();
                System.out.println(Thread.currentThread().getName() + " " + value.get());
            }
        },"A");
        t1.start();
        t1.join();

        new Thread(() -> {
            synchronized (object) {
                int res = value.get();
                while (res == 1) {
                    object.notify();
                }
                value.getAndDecrement();
                System.out.println(Thread.currentThread().getName() + " " + value.get());
            }
        }, "B").start();

    }
}
